package cn.scnu.component;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ZipUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.concurrent.CompletableFuture;

/**
 * TODO
 *
 * @author hqz
 * @className AsyncFun
 * @createTime 2024年12月20日 14:38:00
 */
@Component
@Slf4j
public class AsyncFun {

    @Async("ASYNC_EXECUTOR")
    public CompletableFuture<String> fileZip(String filePath, String parentDir) throws InterruptedException {
        log.info("fileZip:{}", filePath);
        File zip = ZipUtil.zip(filePath);
        String zipFileName = FileUtil.getName(zip);
        String targetFilePath = StrUtil.format("{}/{}", parentDir, zipFileName);
        FileUtil.move(zip, new File(targetFilePath), true);
        return CompletableFuture.completedFuture(targetFilePath);
    }
}
